home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / glibc108.zip / glibc108 / sysdeps / alpha / strlen.c < prev    next >
C/C++ Source or Header  |  1993-12-22  |  2KB  |  55 lines

  1. /* Copyright (C) 1992 Free Software Foundation, Inc.
  2.  
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Library General Public License as
  5. published by the Free Software Foundation; either version 2 of the
  6. License, or (at your option) any later version.
  7.  
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  11. Library General Public License for more details.
  12.  
  13. You should have received a copy of the GNU Library General Public
  14. License along with the GNU C Library; see the file COPYING.LIB.  If
  15. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  16. Cambridge, MA 02139, USA.  */
  17.  
  18. #include <string.h>
  19.  
  20. /* Return the length of the null-terminated string STR.  Scan for
  21.    the null terminator quickly by testing eight bytes at a time.  */
  22.  
  23. size_t
  24. strlen (const char *str)
  25. {
  26.   const char *char_ptr;
  27.   const unsigned long int *longword_ptr;
  28.  
  29.   /* Handle the first few characters by reading one character at a time.
  30.      Do this until STR is aligned on a 8-byte border.  */
  31.   for (char_ptr = str; ((unsigned long int) char_ptr & 7) != 0; ++char_ptr)
  32.     if (*char_ptr == '\0')
  33.       return char_ptr - str;
  34.  
  35.   longword_ptr = (unsigned long int *) char_ptr;
  36.  
  37.   for (;;)
  38.     {
  39.       int mask;
  40.       asm ("cmpbge %1, %2, %0" : "=r" (mask) : "r" (0), "r" (*longword_ptr++));
  41.       if (mask)
  42.     {
  43.       /* Which of the bytes was the zero?  */
  44.  
  45.       const char *cp = (const char *) (longword_ptr - 1);
  46.       int i;
  47.  
  48.       for (i = 0; i < 6; i++)
  49.         if (cp[i] == 0)
  50.           return cp - str + i;
  51.       return cp - str + 7;
  52.     }
  53.     }
  54. }
  55.